﻿using WRJClient.DAL;
using WRJClient.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WRJClient.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WRJClient.ViewModel;

namespace WRJClient.Net
{
    public class UAVMessageProcessor : IMessageProcessor
    {
        public void Process(TopicData topicData)
        {
            switch (topicData.eventId)
            {
                case 1://无人机新建事件
                    UavAddProcess(topicData);
                    break;
                case 2://无人机删除事件
                    UavDelProcess(topicData);
                    break;
                case 3://无人机修改事件
                    UavUpdateProcess(topicData);
                    break;
                case 4://无人机心跳事件
                    UavHeartbeatProcess(topicData);
                    break;
                case 5://无人机状态事件
                    UavStatusUpdateProcess(topicData);
                    break;
                case 6://无人机上线事件
                    UavOnlineProcess(topicData);
                    break;
                case 7://无人机下线事件
                    UavOfflineProcess(topicData);
                    break;
                default:
                    break;
            }
        }

        private void UavAddProcess(TopicData topicData)
        {
            //MessageManager.Instance.Info(string.Format("新建无人机[{0}]!", topicData.id));
        }
        private void UavDelProcess(TopicData topicData)
        {
            //MessageManager.Instance.Info(string.Format("删除无人机[{0}]!", topicData.id));
        }
        private void UavUpdateProcess(TopicData topicData)
        {
            //MessageManager.Instance.Info(string.Format("更新无人机[{0}]!", topicData.id));
        }
        private void UavOnlineProcess(TopicData topicData)
        {
            UAVViewModel vm = UAVManager.Instance.GetById(topicData.id);
            if (vm != null)
            {
                vm.IsOnline = true;
                MessageManager.Instance.Info(string.Format("无人机{0}上线！", vm.StaticData.UavId));
            }
        }
        private void UavOfflineProcess(TopicData topicData)
        {
            UAVViewModel vm = UAVManager.Instance.GetById(topicData.id);
            if (vm != null)
            {
                vm.IsOnline = false;
                MessageManager.Instance.Info(string.Format("无人机{0}下线！", vm.StaticData.UavId));
            }
        }

        public class UavHeartBeat
        {
            public double altitude { get; set; }
            public double airSpeed { get; set; }
            public double groundSpeed { get; set; }
            public double latitude { get; set; }
            public double roll { get; set; }
            public double pitch { get; set; }
            public double verticalSpeed { get; set; }
            public double yaw { get; set; }
            public double longitude { get; set; }
        };
        private void UavStatusUpdateProcess(TopicData topicData)
        {
            UAVViewModel vm = UAVManager.Instance.GetById(topicData.id);
            if (vm != null)
            {
                UavHeartBeat heartbeat = JsonConvert.DeserializeObject<UavHeartBeat>(topicData.data);
                TelemetryModel telemetry = new TelemetryModel
                {
                    Altitude = heartbeat.altitude,
                    AirSpeed = heartbeat.airSpeed,
                    GroundSpeed = heartbeat.groundSpeed,
                    Latitude = heartbeat.latitude,
                    Roll = heartbeat.roll,
                    Pitch = heartbeat.pitch,
                    VerticalSpeed = heartbeat.verticalSpeed,
                    Yaw = heartbeat.yaw,
                    Longitude = heartbeat.longitude
                };
                vm.Telemetry = telemetry;
            }
        }
        private void UavHeartbeatProcess(TopicData topicData)
        {
            UAVViewModel vm = UAVManager.Instance.GetById(topicData.id);
            if (vm != null)
            {
                vm.HeartBeat();
            }
        }
    }
}
